package com.hjr.calculator;

import com.hjr.calculator.algorithm.AddAlgorithm;
import com.hjr.calculator.algorithm.DivideAlgorithm;
import com.hjr.calculator.algorithm.MultiplyAlgorithm;
import com.hjr.calculator.algorithm.SubtractAlgorithm;

import java.util.HashMap;
import java.util.Map;

public class CalcStrategyContext {

    private final Map<String, CalcStrategy> calcStrategyMap;

    public CalcStrategyContext() {
        calcStrategyMap = new HashMap<>();
        calcStrategyMap.put(CalcOp.ADD.getOp(), new AddAlgorithm());
        calcStrategyMap.put(CalcOp.SUBTRACT.getOp(), new SubtractAlgorithm());
        calcStrategyMap.put(CalcOp.MULTIPLY.getOp(), new MultiplyAlgorithm());
        calcStrategyMap.put(CalcOp.DIVIDE.getOp(), new DivideAlgorithm());
    }

    private static CalcStrategyContext instance;
    public static synchronized CalcStrategyContext getInstance() {
        if (instance == null) {
            instance = new CalcStrategyContext();
        }
        return instance;
    }

    public CalcStrategy getByOp(String op) {
        if (!calcStrategyMap.containsKey(op))
            throw new UnsupportedOperationException(String.format("不支持计算操作符[%s]", op));
        return calcStrategyMap.get(op);
    }


}